package cn.xiu.dang.dao.impl;

import java.sql.ResultSet;

import cn.xiu.dang.dao.UserDao;
import cn.xiu.dang.pojo.User;
import cn.xiu.dang.util.BaseDao;
/**
 * 功能实现类
 * @author Administrator
 *
 */
public class UserDaoImpl extends BaseDao implements UserDao{
    	
	@Override
	/**
	 * 主人注册
	 * @return result
	 */
	public int save(User user) throws Exception{
		String sql="INSERT INTO `user`(username,userpass,email,sex)VALUES(?,?,?,?)";
		Object[] prarms={user.getUserName(),user.getUserPass(),user.getEmail(),user.getSex()};
			BaseDao dao=new BaseDao();
			int result=dao.executeUpdate(sql, prarms);
		return result;
	}

	@Override
	/**
	 * 从数据库中通过用户名称获取用户对象
	 */
	public User findUserByUserName(String email) {
		//sql语句
		String sql="SELECT uid, username ,userpass,email,sex FROM `user` WHERE email=?";
		Object[] params={email};
		BaseDao dao=new BaseDao();
		User user=null;
		try {
			ResultSet rs=dao.executeQuery(sql, params);
			while(rs.next()){
				user=new User();
				user.setuId(rs.getInt("uid"));
				user.setUserName(rs.getString("username"));
				user.setUserPass(rs.getString("userpass"));
				user.setEmail(rs.getString("email"));
				user.setSex(rs.getString("sex"));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

}
